//
//  ByeCodeViewController.m
//  PlayerWorld
//
//  Created by 汪的一声就哭了！ on 16/6/17.
//  Copyright © 2016年 刘成. All rights reserved.
//
#import "ByeCodeViewController.h"
#import "UIKit+AFNetworking.h"
#import "ByeCodeTableViewCell.h"
#import "ByeCodeCollectionViewCell.h"
#import "ByeCodeHeader.h"
#import "ByeCodeFooter.h"
#import "PLFiveObject.h"
#import "ConfirmationViewController.h"
#import "G_P_Object.h"
#import "ILView.h"
@interface ByeCodeViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSTimer *_timer;
@end

@implementation ByeCodeViewController
{
    NSArray *_listArr;
    NSMutableArray *_detailArr;
    NSMutableArray *_flagArr;
    NSMutableString *_string;
    ByeCodeHeader *_headerView;
    ByeCodeFooter *_footer;
    NSString *_footString;
    NSString *_headString;
    NSString *_headTowStr;
    NSString *_firstNumStr;
    NSInteger secondsCountDown;
    NSMutableArray *_flagArray;
    NSArray *_array;
    NSArray *_typeArr;
    BOOL _Type;
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"Game" forKey:@"Model"];
    [parametersDic setObject:@"GetTheIssueAndTime" forKey:@"Action"];
    [parametersDic setObject:self.G_N_Id forKey:@"Id"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _model = [[PLFiveObject alloc] init];
        [_model setValuesForKeysWithDictionary:dic];
        secondsCountDown = [_model.overtime integerValue];
        if (secondsCountDown>0) {
            __timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        }
        else{
            [self obtainData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}
// 倒计时方法
-(void)timeFireMethod{
    //倒计时-1
    secondsCountDown--;
    //修改倒计时标签现实内容
    //    labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];
    NSString *secondValue = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(secondsCountDown/60/60)%60,(secondsCountDown/60)%60,secondsCountDown%60];
    _headerView.timeLabel.text= secondValue;
    //当倒计时到0时，做需要的操作
    if(secondsCountDown==0){
        [__timer invalidate];
        [self obtainData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self obtainData];
    _typeArr = @[@"全",@"大",@"小",@"奇",@"偶",@"清"];
    self.title = self.name;
//    _footString = [self.G_P_Model.G_P_NumberName substringFromIndex:self.G_P_Model.G_P_NumberName.length-1];
    NSArray *headStrArr = [self.G_P_Model.G_P_NumberName componentsSeparatedByString:@"|"];
    _Type = [[headStrArr objectAtIndex:2] boolValue];
    _headString = [headStrArr firstObject];
    _footString = [headStrArr lastObject];
    _headTowStr = [[[headStrArr objectAtIndex:1] componentsSeparatedByString:@"="] firstObject];
    _listArr = [self.G_P_Model.G_P_NumberName componentsSeparatedByString:@"#"];
    _detailArr = [NSMutableArray array];
    _flagArr = [NSMutableArray array];
    for (NSString *string in _listArr) {
        NSArray *arr = [string componentsSeparatedByString:@"="];
        NSString *nameStr = [[[arr firstObject] componentsSeparatedByString:@"|"] lastObject];
        NSString *str = [[[arr lastObject] componentsSeparatedByString:@"|"] firstObject];
        NSArray *codeArr = [str componentsSeparatedByString:@","];
        _firstNumStr = [codeArr firstObject];
        NSDictionary *dic = @{nameStr:codeArr};
        [_detailArr addObject:dic];
        NSMutableArray *array = [NSMutableArray array];
        for (NSInteger i = 0; i < codeArr.count; i++) {
            NSNumber *num = [NSNumber numberWithBool:NO];
            [array addObject:num];
        }
        [_flagArr addObject:array];
    }
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-50-64) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = [UIColor colorWithRed:233.0/255.0 green:234.0/255.0 blue:235.0/255.0 alpha:1];
    
    [self.myTableView registerClass:[ByeCodeTableViewCell class] forCellReuseIdentifier:@"nihao"];
    
    _headerView = [[ByeCodeHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
    _headerView.playLabel.text = self.G_P_Model.G_P_Name;
    self.myTableView.tableHeaderView = _headerView;
    [_headerView.chooseBtn addTarget:self action:@selector(randomChooseAction:) forControlEvents:UIControlEventTouchUpInside];
    _array = @[@"万",@"千",@"百",@"十",@"个"];
    _flagArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 5; i++) {
        ILView *view = [[ILView alloc] initWithFrame:CGRectMake(100+i*((self.view.bounds.size.width-100)/5), 10, (self.view.bounds.size.width-100)/5, 30)];
        view.label.font = [UIFont systemFontOfSize:12];
        view.label.text = [NSString stringWithFormat:@"%@位",_array[i]];
        view.tag = 1000+i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(choosePlaceAction:)];
        [view addGestureRecognizer:tap];
        if ([self.G_P_Model.G_P_Name isEqualToString:@"任三和值"]) {
            [_headerView addSubview:view];
        }
        [_flagArray addObject:[NSNumber numberWithBool:NO]];
    }
    _footer = [[ByeCodeFooter alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-50-64, self.view.bounds.size.width, 50)];
    [self.view addSubview:_footer];
    [_footer.trashBtn addTarget:self action:@selector(trashAction:) forControlEvents:UIControlEventTouchUpInside];
    [_footer.sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)choosePlaceAction:(UITapGestureRecognizer *)tap
{
    ILView *view = (ILView *)tap.view;
    NSInteger integer = view.tag - 1000;
    if ([view.imageView.image isEqual:[UIImage imageNamed:@"quan"]]) {
        view.imageView.image = [UIImage imageNamed:@"quan1"];
        [_flagArray setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:integer];
    }
    else{
        view.imageView.image = [UIImage imageNamed:@"quan"];
        [_flagArray setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:integer];
    }
    [self handleNextArr];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ByeCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nihao"];
    cell.collectionView.delegate = self;
    cell.collectionView.dataSource = self;
    cell.collectionView.tag = indexPath.row + 1000;
    [cell.collectionView registerClass:[ByeCodeCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    [cell.collectionView reloadData];
    NSDictionary *dic = [_detailArr objectAtIndex:indexPath.row];
    cell.label.text = [dic.allKeys firstObject];
    NSDictionary *dic1 = [_detailArr objectAtIndex:indexPath.row];
    NSArray *arr = [dic1.allValues firstObject];
    if (([self isPureInt:arr.firstObject] && [arr.firstObject length] > 2) || (![self isPureInt:arr.firstObject] && [arr.firstObject length] > 1)) {
        cell.flow.itemSize = CGSizeMake(66, 37);
    }
    else{
        cell.flow.itemSize = CGSizeMake(36, 37);
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_detailArr objectAtIndex:indexPath.row];
    NSArray *arr = [dic.allValues firstObject];
    NSInteger integer;
    if (([self isPureInt:arr.firstObject] && [arr.firstObject length] > 2) || (![self isPureInt:arr.firstObject] && [arr.firstObject length] > 1)) {
        integer = (self.view.bounds.size.width - 70) / 76;
    }else{
        integer = (self.view.bounds.size.width - 70) / 46;
    }
    NSInteger line;
    if (arr.count % integer == 0) {
        line = arr.count/integer;
    }
    else{
        line = arr.count/integer + 1;
    }
    integer = (self.view.bounds.size.width - 70) / 46;
    NSInteger aline;
    if (6 % integer == 0) {
        aline = 6/integer;
    }
    else{
        aline = 6/integer + 1;
    }
    if (!_Type) {
        return 37*line + 10*(line+1);
    }
    return 37*line + 10*(line+1) + 37*aline + 10*(aline + 1);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return CGSizeMake(0, 10);
    }
    else{
        return CGSizeMake(0, 0);
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 1) {
        return _typeArr.count;
    }
    NSDictionary *dic = [_detailArr objectAtIndex:collectionView.tag - 1000];
    NSArray *arr = [dic.allValues firstObject];
    return arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ByeCodeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSDictionary *dic = [_detailArr objectAtIndex:collectionView.tag - 1000];
        NSString *str = [[dic.allValues firstObject] objectAtIndex:indexPath.item];
        NSArray *arr = [_flagArr objectAtIndex:collectionView.tag-1000];
        BOOL flag = [[arr objectAtIndex:indexPath.item] boolValue];
        if (!flag) {
            if (([self isPureInt:str] && [str length] > 2) || (![self isPureInt:str] && [str length] > 1)) {
                cell.imageView.image = [UIImage imageNamed:@"icon_big_ball"];
            }
            else{
                cell.imageView.image = [UIImage imageNamed:@"icon_ball"];
            }
            cell.label.textColor = [UIColor colorWithRed:36/255.0 green:11/255.0 blue:54/255.0 alpha:1];
        }
        else{
            if (([self isPureInt:str] && [str length] > 2) || (![self isPureInt:str] && [str length] > 1)) {
                cell.imageView.image = [UIImage imageNamed:@"icon_ball_pink_b"];
            }
            else{
                cell.imageView.image = [UIImage imageNamed:@"icon_ball_pink"];
            }
            cell.label.textColor = [UIColor whiteColor];
        }
        cell.label.text = str;
        cell.label.font = [UIFont systemFontOfSize:17];
    }
    else{
        cell.imageView.image = [UIImage imageNamed:@"icon_ball(2)"];
        cell.label.text = [_typeArr objectAtIndex:indexPath.item];
        cell.label.font = [UIFont systemFontOfSize:17];
        cell.label.textColor = [UIColor colorWithRed:36/255.0 green:11/255.0 blue:54/255.0 alpha:1];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if ([[[_flagArr objectAtIndex:collectionView.tag-1000] objectAtIndex:indexPath.item] boolValue]) {
            [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:indexPath.item];
        }
        else{
            [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:indexPath.item];
        }
        [self handleNextArr];
        [collectionView reloadData];
    }
    else{
        NSInteger inte = [[_flagArr objectAtIndex:collectionView.tag-1000] count];
        if (indexPath.item == 0) {
            for (NSInteger i = 0; i < inte; i++) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
            }
        }
        if (indexPath.item == 1) {
            for (NSInteger i = 0; i < inte/2; i++) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
            }
            for (NSInteger i = inte/2; i < inte; i++) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
            }
        }
        if (indexPath.item == 2) {
            for (NSInteger i = 0; i < inte/2; i++) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
            }
            for (NSInteger i = inte/2; i < inte; i++) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
            }
        }
        if (indexPath.item == 3) {
            if ([self.name containsString:@"11选5"] || [self.name containsString:@"赛车"]) {
                for (NSInteger i = 1; i < inte; i = i + 2) {
                    [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
                }
                for (NSInteger i = 0; i < inte; i = i + 2) {
                    [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
                }
            }
            else{
            for (NSInteger i = 0; i < inte; i = i + 2) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
            }
            for (NSInteger i = 1; i < inte; i = i + 2) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
            }
            }
        }
        if (indexPath.item == 4) {
            if ([self.name containsString:@"11选5"] || [self.name containsString:@"赛车"]) {
                for (NSInteger i = 0; i < inte; i = i + 2) {
                    [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
                }
                for (NSInteger i = 1; i < inte; i = i + 2) {
                    [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
                }
            }
            else{
            for (NSInteger i = 0; i < inte; i = i + 2) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:i];
            }
            for (NSInteger i = 1; i < inte; i = i + 2) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
            }
            }
        }
        if (indexPath.item == 5) {
            for (NSInteger i = 0; i < inte; i++) {
                [[_flagArr objectAtIndex:collectionView.tag-1000] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
            }
        }
        [self handleNextArr];
        [collectionView reloadData];
    }
}
- (void)reloadFlagArray
{
    _flagArr = [NSMutableArray array];
    for (NSString *string in _listArr) {
        NSArray *arr = [string componentsSeparatedByString:@"="];
        NSString *str = [[[arr lastObject] componentsSeparatedByString:@"|"] firstObject];
        NSArray *codeArr = [str componentsSeparatedByString:@","];
        NSMutableArray *array = [NSMutableArray array];
        for (NSInteger i = 0; i < codeArr.count; i++) {
            NSNumber *num = [NSNumber numberWithBool:NO];
            [array addObject:num];
        }
        [_flagArr addObject:array];
    }
}
- (void)handleNextArr
{
//    _nextArr = [NSMutableArray array];
    _string = [NSMutableString string];
    NSString *muStr;
    if ([self.G_P_Model.G_P_Name isEqualToString:@"任三和值"]) {
        for (NSString *string in _array) {
            NSInteger inte = [_array indexOfObject:string];
            if ([_flagArray[inte] boolValue]) {
                [_string appendString:string];
            }
        }
        muStr = [NSString stringWithFormat:@"%@",_string];
        [_string appendString:@":"];
    }
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSInteger i = 0; i < _flagArr.count; i++) {
        NSMutableArray *arr = [_flagArr objectAtIndex:i];
        NSMutableArray *smallArr = [NSMutableArray array];
        for (NSInteger j = 0; j < arr.count ; j++) {
            NSNumber *num = [arr objectAtIndex:j];
            if ([num boolValue]) {
                [_string appendFormat:@"%@,",[[[[_detailArr objectAtIndex:i] allObjects] firstObject] objectAtIndex:j]];
                [smallArr addObject:[[[[_detailArr objectAtIndex:i] allObjects] firstObject] objectAtIndex:j]];
            }
        }
        [bigArray addObject:smallArr];
        if (_string.length>0) {
            if ([[_string substringWithRange:NSMakeRange(_string.length-1, 1)] isEqualToString:@","]) {
                [_string deleteCharactersInRange:NSMakeRange(_string.length-1, 1)];
            }
        }
        [_string appendString:@"|"];
    }
    [_string deleteCharactersInRange:NSMakeRange(_string.length-1, 1)];
    if ([self.G_P_Model.G_P_Name containsString:@"三和值"]) {
        NSArray *addArray = @[@1,@3,@6,@10,@15,@21,@28,@36,@45,@55,@63,@69,@73,@75,@75,@73,@69,@63,@55,@45,@36,@28,@21,@15,@10,@6,@3,@1];
        NSInteger moneyNum = 0;
        for (NSArray *smallArray in bigArray) {
            for (NSString *numstr in smallArray) {
                moneyNum += [[addArray objectAtIndex:[numstr integerValue]] integerValue];
            }
        }
        _footer.numLabel1.text = [NSString stringWithFormat:@"%ld",(long)moneyNum];
        _footer.numLabel3.text = [NSString stringWithFormat:@"%ld.00",(long)moneyNum];
        if ([self.G_P_Model.G_P_Name isEqualToString:@"任三和值"]) {
            if (muStr.length != 3) {
                _footer.numLabel1.text = [NSString stringWithFormat:@"%d",0];
                _footer.numLabel3.text = [NSString stringWithFormat:@"%d.00",0];
            }
        }
    }
    else if ([_headTowStr isEqualToString:@"Size"]) {
        for (NSArray *smallArray in bigArray) {
            if (smallArray.count != 1) {
                _footer.numLabel1.text = [NSString stringWithFormat:@"%d",0];
                _footer.numLabel3.text = [NSString stringWithFormat:@"%d.00",0];
            }
            else{
                _footer.numLabel1.text = [NSString stringWithFormat:@"%d",1];
                _footer.numLabel3.text = [NSString stringWithFormat:@"%d.00",1];
            }
        }
    }
    else if (([self isPureInt:_firstNumStr] && _detailArr.count == 1 && ![_headTowStr containsString:@"和值"] && ![_headTowStr containsString:@"不定位"]) || ([_headTowStr containsString:@"任选"] && _detailArr.count == 1)) {
        NSInteger lineNum;
        if ([self isPureInt:_headString]) {
            lineNum = [_headString integerValue];
        }
        else{
            lineNum = _headString.length;
        }
        if ([_headTowStr containsString:@"任选一"]) {
            lineNum = 1;
        }
        if ([_headTowStr containsString:@"任选二"]) {
            lineNum = 2;
        }
        if ([_headTowStr containsString:@"任选三"]) {
            lineNum = 3;
        }
        if ([_headTowStr containsString:@"任选四"]) {
            lineNum = 4;
        }
        if ([_headTowStr containsString:@"任选五"]) {
            lineNum = 5;
        }
        if ([_headTowStr containsString:@"任选六"]) {
            lineNum = 6;
        }
        if ([_headTowStr containsString:@"任选七"]) {
            lineNum = 7;
        }
        if ([_headTowStr containsString:@"任选八"]) {
            lineNum = 8;
        }
//        NSLog(@"%ld",lineNum);
        for (NSArray *smallArray in bigArray) {
            if (smallArray.count >= lineNum) {
//一行多选的时候 排列组合（Cn/m = m!/((m-n)! * n!) ）
                NSInteger countNum = [self serialMultyplyWithInt:smallArray.count] /[self serialMultyplyWithInt:lineNum]/ [self serialMultyplyWithInt:smallArray.count - lineNum];
                _footer.numLabel1.text = [NSString stringWithFormat:@"%ld",(long)countNum];
                _footer.numLabel3.text = [NSString stringWithFormat:@"%ld.00",(long)countNum];
            }
            else{
                _footer.numLabel1.text = [NSString stringWithFormat:@"%d",0];
                _footer.numLabel3.text = [NSString stringWithFormat:@"%d.00",0];
            }
        }
        if ([_headTowStr containsString:@"组三"]) {
            lineNum = 2;
            for (NSArray *smallArray in bigArray) {
                if (smallArray.count >= lineNum) {
                    NSInteger countNum = smallArray.count * (smallArray.count - 1);
                    _footer.numLabel1.text = [NSString stringWithFormat:@"%ld",(long)countNum];
                    _footer.numLabel3.text = [NSString stringWithFormat:@"%ld.00",(long)countNum];
                }
            }
        }
    }
    else if ([self.G_P_Model.G_P_Name containsString:@"定位胆"]) {
        NSInteger countNum = 0;
        for (NSArray *smallArray in bigArray) {
            countNum = countNum + smallArray.count;
        }
        _footer.numLabel1.text = [NSString stringWithFormat:@"%ld",(long)countNum];
        _footer.numLabel3.text = [NSString stringWithFormat:@"%ld.00",(long)countNum];
    }
    else{
        if ([_footString integerValue] > _flagArr.count) {
            _footString = [NSString stringWithFormat:@"%ld",(unsigned long)_flagArr.count];
        }
//        NSLog(@"else:%@",_footString);
    NSInteger countNum = 1;
        NSInteger nullNum = 0;
        for (NSArray *smallArray in bigArray) {
            if (smallArray.count == 0) {
                nullNum++;
            }
            else{
                countNum = countNum * smallArray.count;
            }
        }
        if (nullNum != _flagArr.count - [_footString integerValue]) {
            countNum = 0;
        }
    _footer.numLabel1.text = [NSString stringWithFormat:@"%ld",(long)countNum];
    _footer.numLabel3.text = [NSString stringWithFormat:@"%ld.00",(long)countNum];
    }
//    NSLog(@"%@",_string);
}
#pragma mark - 阶乘
- (NSInteger)serialMultyplyWithInt:(NSInteger)integer
{
    if (integer == 0) {
        return 1;
    }
    NSInteger mul = 1;
    for (NSInteger i = integer; i > 0; i--) {
        mul = mul*i;
    }
    return mul;
}
#pragma mark - 判断字符串是不是数字
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}
- (void)randomChooseAction:(UIButton *)btn
{
    [self reloadFlagArray];
    if (([self isPureInt:_firstNumStr] && _detailArr.count == 1 && ![_headTowStr containsString:@"和值"] && ![_headTowStr containsString:@"不定位"]) || ([_headTowStr containsString:@"任选"] && _detailArr.count == 1)) {
        NSInteger lineNum;
        if ([self isPureInt:_headString]) {
            lineNum = [_headString integerValue];
        }
        else{
            lineNum = _headString.length;
        }
        if ([_headTowStr containsString:@"任选一"]) {
            lineNum = 1;
        }
        if ([_headTowStr containsString:@"任选二"]) {
            lineNum = 2;
        }
        if ([_headTowStr containsString:@"任选三"]) {
            lineNum = 3;
        }
        if ([_headTowStr containsString:@"任选四"]) {
            lineNum = 4;
        }
        if ([_headTowStr containsString:@"任选五"]) {
            lineNum = 5;
        }
        if ([_headTowStr containsString:@"任选六"]) {
            lineNum = 6;
        }
        if ([_headTowStr containsString:@"任选七"]) {
            lineNum = 7;
        }
        if ([_headTowStr containsString:@"任选八"]) {
            lineNum = 8;
        }
        if ([_headTowStr containsString:@"组三"]) {
            lineNum = 2;
        }
        NSMutableArray *randomArr = [NSMutableArray array];
        for (NSInteger i = 0; i < lineNum; i++) {
            NSInteger random = arc4random() % [[_flagArr firstObject] count];
            //            NSLog(@"你好");
            if ([randomArr containsObject:[NSNumber numberWithInteger:random]]) {
                //                NSLog(@"跳出一重循环");
                i--;
                continue;
            }
            [randomArr addObject:[NSNumber numberWithInteger:random]];
        }
        for (NSNumber *random in randomArr) {
            [[_flagArr objectAtIndex:0] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:[random integerValue]];
        }
    }
    else{
        if ([self.G_P_Model.G_P_Name containsString:@"定位胆"]) {
            _footString = @"1";
        }
        if ([_footString integerValue] > _flagArr.count) {
            _footString = [NSString stringWithFormat:@"%ld",_flagArr.count];
        }
        for (NSInteger i = 0; i < _flagArr.count; i++) {
            NSInteger count = [[_flagArr objectAtIndex:i] count];
            NSInteger random = arc4random() % count;
            [[_flagArr objectAtIndex:i] setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:random];
        }
        //    if ([_footString isEqualToString:@"1|4|4"]) {
        NSMutableArray *randomArr = [NSMutableArray array];
        for (NSInteger i = 0; i < _flagArr.count - [_footString integerValue]; i++) {
            NSInteger random = arc4random() % _flagArr.count;
            if ([randomArr containsObject:[NSNumber numberWithInteger:random]]) {
                i--;
                continue;
            }
            [randomArr addObject:[NSNumber numberWithInteger:random]];
        }
        for (NSNumber *random in randomArr) {
            for (NSInteger i = 0; i < [[_flagArr objectAtIndex:[random integerValue]] count]; i++) {
                [[_flagArr objectAtIndex:[random integerValue]] setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:i];
            }
        }
    }
    [self handleNextArr];
    [self.myTableView reloadData];
}
- (void)trashAction:(UIButton *)btn
{
    [self reloadFlagArray];
    [self handleNextArr];
    [self.myTableView reloadData];
}
#pragma mark - 确认
- (void)sureAction:(UIButton *)btn
{
    if (![_footer.numLabel1.text isEqualToString:@"0"]) {
        ConfirmationViewController *cVC= [[ConfirmationViewController alloc] init];
        cVC.G_N_Id = _G_N_Id;
        cVC.G_P_Model = _G_P_Model;
        cVC.Issue = _model.Then_Issue;
        cVC.money = [_footer.numLabel1.text integerValue];
        cVC.string = _string;
        [self.navigationController pushViewController:cVC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
